<?php

namespace app\model;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class User extends UserBase
{
    public function onAfterInsert()
    {
        $payload = [
            "uid" => $this->id,
        ];
        $this->token = JWT::encode($payload, 'dalanka', "HS256");
        $this->save();
    }

    public function onBeforeWrite()
    {
        if ($this->password) {
            $this->password = password_hash($this->password, PASSWORD_DEFAULT);
        }
    }

    public function scopeUser()
    {
        $this->where('user_id', request()->user->id);
    }
}
